<?
include("session.php"); 
include("database.php");

$action = $_GET['action'];

 if($action == "get") {
	$sql = "select * from hngj_agent where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "del") {
	$sql = "delete from hngj_agent where id=".$_GET['id']; 
	$db->query($sql);
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['agent_group'])) {
		$condition .= " and agent_group='".$_GET['agent_group']."'";
	}
	$sql = "select id,agent_id,agent_name,agent_group,create_time from hngj_agent where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("agent_id"=>"blue_color_mapper","agent_group"=>"red_color_mapper"));

} else if($action == "add") { 
	unset($_POST['id']);
	$_POST['create_time'] = date("Y-m-d H:i:s");
	$db->doInsert('hngj_agent', $_POST);
	echo 'ok';

}  else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate('hngj_agent', $_POST, "id=$id");
	echo 'ok';

} else if($action == "get_group_options") {
	$arr = array();
	$db->query("select agent_group from hngj_agent group by agent_group");
	while($db->moveNext()) {
		$arr[] = $db->f("agent_group") .",". $db->f("agent_group");
	}
	echo join("$", $arr);

} else if($action == "get_agent_by_group") {
	$arr = array();
	$db->query("select agent_id from hngj_agent where agent_group='".$_GET['agent_group']."'");
	while($db->moveNext()) {
		$arr[] = $db->f("agent_id");
	}
	echo join("$", $arr);

} else if($action == "get_last_agent_group") {
	echo $db->getOneValue("select agent_group from hngj_agent order by id desc");
}

?>